#include "ActionDessinResize.h"
#include "Dessin.h"


ActionDessinResize::ActionDessinResize() : ActionDessin(){}

void ActionDessinResize::OnMousePressed(Dessin* d,wxMouseEvent &event){

    debut_mouv_x=event.m_x;
    debut_mouv_y=event.m_y;
    selected =true;
    d->SetCursor(wxCursor(wxCURSOR_SIZENWSE));

}

void ActionDessinResize::OnRelease(Dessin* d,wxMouseEvent &event){
    selected=false;
    d->SetCursor(wxCursor(wxCURSOR_ARROW));
}

void ActionDessinResize::OnMotion(Dessin* d,wxMouseEvent &event){
    if(selected){
        resize(d,debut_mouv_x,debut_mouv_y,event.m_x,event.m_y);
        if(ActionDessin::anyEdgeReached(d)) correctDimension(d);
        d->Refresh();
        debut_mouv_x=event.m_x;
        debut_mouv_y=event.m_y;
     }
}

void ActionDessinResize::correctDimension(Dessin* d){
    long x1,y1,x2,y2;
    x1 = d->GetPosition().x;
    y1 = d->GetPosition().y;
    x2 = x1+(d->GetSize().GetWidth());
    y2 = y1+(d->GetSize().GetHeight());
    if(ActionDessin::nEdgeReached(d)) y1=0;
    if(ActionDessin::wEdgeReached(d)) x1=0;
    if(ActionDessin::eEdgeReached(d)) x2=(d->GetParent()->GetSize()).GetWidth();
    if(ActionDessin::sEdgeReached(d)) y2=(d->GetParent()->GetSize()).GetHeight();
    d-> SetSize(x1,y1,x2-x1,y2-y1);

}

void ActionDessinResize::resize(Dessin* d,long entre_x1,long entre_y1,long entre_x2,long entre_y2){
    long x1,y1,x2,y2,temp;
    x1 = d->GetPosition().x;
    y1 = d->GetPosition().y;
    x2 = x1+(d->GetSize().GetWidth());
    y2 = y1+(d->GetSize().GetHeight());

    x2+=(entre_x2-entre_x1);
    y2+=(entre_y2-entre_y1);

    if(x2<x1){
        temp=x2;
        x1=temp;
        x2=x1;
    }
    if(y2<y1){
        temp=x2;
        x1=temp;
        x2=x1;
    }

    d->SetSize(x1,y1,x2-x1,y2-y1);
    d->Refresh();

}

void ActionDessinResize::OnTimer(Dessin* d,wxTimerEvent& event){}

